﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.Order
{
    public class T_PUB_ORDER
    {

        /// <summary>
        /// 获取订单跟踪提醒
        /// </summary>
        /// <param name="branch_id"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public DataTable GetRemindOrderList(int branch_id, int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ORDER_ID,CUSTOMER_ID,Num,PROJECT_IDS,STYLE_IDS,Reception,RealToal,Total,DingJin,Type,Other,IsDelete,DeleteTime,DeleteUser,CheckItem,Source,OrderDate,DateOther,BranchID,MemorialDay,MemorialType,DeleteReason,HotelManage,WeedPrice from T_PUB_ORDER ");
            strSql.Append("where BranchID=" + branch_id + " Reception=" + user_id);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <param name="coustomerModel"></param>
        public void AddOrder(Model.Order.T_PUB_ORDER model, Model.View.T_PUB_CUSTOMERINFO coustomerModel, Dictionary<string, string> ProjectID_No)
        {
            ArrayList strArry = new ArrayList();

            #region 添加客户信息

            StringBuilder CstrSql = new StringBuilder();
            StringBuilder CstrSql1 = new StringBuilder();
            StringBuilder CstrSql2 = new StringBuilder();
            string strCoustomerID = System.Guid.NewGuid().ToString();
            CstrSql1.Append("CUSTOMER_ID,");
            CstrSql2.Append("'" + strCoustomerID + "',");
            if (coustomerModel.GROOM_NAME != null)
            {
                CstrSql1.Append("GROOM_NAME,");
                CstrSql2.Append("'" + coustomerModel.GROOM_NAME + "',");
            }
            if (coustomerModel.BRIDE_NAME != null)
            {
                CstrSql1.Append("BRIDE_NAME,");
                CstrSql2.Append("'" + coustomerModel.BRIDE_NAME + "',");
            }
            if (coustomerModel.GROOMPOST != null)
            {
                CstrSql1.Append("GROOMPOST,");
                CstrSql2.Append("'" + coustomerModel.GROOMPOST + "',");
            }
            if (coustomerModel.BRIDEPOST != null)
            {
                CstrSql1.Append("BRIDEPOST,");
                CstrSql2.Append("'" + coustomerModel.BRIDEPOST + "',");
            }
            if (coustomerModel.GROOMPHONE != null)
            {
                CstrSql1.Append("GROOMPHONE,");
                CstrSql2.Append("'" + coustomerModel.GROOMPHONE + "',");
            }
            if (coustomerModel.BRIDEPHONE != null)
            {
                CstrSql1.Append("BRIDEPHONE,");
                CstrSql2.Append("'" + coustomerModel.BRIDEPHONE + "',");
            }
            if (coustomerModel.GROOMQQ != null)
            {
                CstrSql1.Append("GROOMQQ,");
                CstrSql2.Append("'" + coustomerModel.GROOMQQ + "',");
            }
            if (coustomerModel.BRIDEQQ != null)
            {
                CstrSql1.Append("BRIDEQQ,");
                CstrSql2.Append("'" + coustomerModel.BRIDEQQ + "',");
            }
            if (coustomerModel.GROOMAGE != null)
            {
                CstrSql1.Append("GROOMAGE,");
                CstrSql2.Append("" + coustomerModel.GROOMAGE + ",");
            }
            if (coustomerModel.BRIDEAGE != null)
            {
                CstrSql1.Append("BRIDEAGE,");
                CstrSql2.Append("" + coustomerModel.BRIDEAGE + ",");
            }
            if (coustomerModel.HOUSEADDRESS != null)
            {
                CstrSql1.Append("HOUSEADDRESS,");
                CstrSql2.Append("'" + coustomerModel.HOUSEADDRESS + "',");
            }
            if (coustomerModel.WEDDING != null)
            {
                CstrSql1.Append("WEDDING,");
                CstrSql2.Append("'" + coustomerModel.WEDDING + "',");
            }
            if (coustomerModel.BRIDEADDRESS != null)
            {
                CstrSql1.Append("BRIDEADDRESS,");
                CstrSql2.Append("'" + coustomerModel.BRIDEADDRESS + "',");
            }
            if (coustomerModel.MARRIAGECAR != null)
            {
                CstrSql1.Append("MARRIAGECAR,");
                CstrSql2.Append("'" + coustomerModel.MARRIAGECAR + "',");
            }
            if (coustomerModel.TIME != null)
            {
                CstrSql1.Append("TIME,");
                CstrSql2.Append("'" + coustomerModel.TIME + "',");
            }
            if (coustomerModel.HOTEL != null)
            {
                CstrSql1.Append("HOTEL,");
                CstrSql2.Append("'" + coustomerModel.HOTEL + "',");
            }
            if (coustomerModel.TABLENUM != null)
            {
                CstrSql1.Append("TABLENUM,");
                CstrSql2.Append("" + coustomerModel.TABLENUM + ",");
            }
            if (coustomerModel.RANK != null)
            {
                CstrSql1.Append("RANK,");
                CstrSql2.Append("" + coustomerModel.RANK + ",");
            }
            if (coustomerModel.ELDER != null)
            {
                CstrSql1.Append("ELDER,");
                CstrSql2.Append("" + coustomerModel.ELDER + ",");
            }
            if (coustomerModel.YOUNGER != null)
            {
                CstrSql1.Append("YOUNGER,");
                CstrSql2.Append("" + coustomerModel.YOUNGER + ",");
            }
            CstrSql.Append("insert into T_PUB_CUSTOMERINFO(");
            CstrSql.Append(CstrSql1.ToString().Remove(CstrSql1.Length - 1));
            CstrSql.Append(")");
            CstrSql.Append(" values (");
            CstrSql.Append(CstrSql2.ToString().Remove(CstrSql2.Length - 1));
            CstrSql.Append(")");
            strArry.Add(CstrSql);

            #endregion

            #region 添加订单

            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();


            strSql1.Append("CUSTOMER_ID,");
            strSql2.Append("'" + strCoustomerID + "',");

            strSql1.Append("ORDER_ID,");
            strSql2.Append("'" + model.ORDER_ID + "',");

            if (model.Num != null)
            {
                strSql1.Append("Num,");
                strSql2.Append("'" + model.Num + "',");
            }
            if (model.PROJECT_IDS != null)
            {
                strSql1.Append("PROJECT_IDS,");
                strSql2.Append("'" + model.PROJECT_IDS + "',");
            }
            if (model.STYLE_IDS != null)
            {
                strSql1.Append("STYLE_IDS,");
                strSql2.Append("'" + model.STYLE_IDS + "',");
            }
            if (model.Reception != null)
            {
                strSql1.Append("Reception,");
                strSql2.Append("" + model.Reception + ",");
            }
            if (model.RealToal != null)
            {
                strSql1.Append("RealToal,");
                strSql2.Append("'" + model.RealToal + "',");
            }
            if (model.Total != null)
            {
                strSql1.Append("Total,");
                strSql2.Append("'" + model.Total + "',");
            }
            if (model.DingJin != null)
            {
                strSql1.Append("DingJin,");
                strSql2.Append("'" + model.DingJin + "',");
            }
            if (model.Type != null)
            {
                strSql1.Append("Type,");
                strSql2.Append("'" + model.Type + "',");
            }
            if (model.Other != null)
            {
                strSql1.Append("Other,");
                strSql2.Append("'" + model.Other + "',");
            }
            if (model.Source != null)
            {
                strSql1.Append("Source,");
                strSql2.Append("'" + model.Source + "',");
            }
            if (model.OrderDate != null)
            {
                strSql1.Append("OrderDate,");
                strSql2.Append("'" + model.OrderDate + "',");
            }
            if (model.DateOther != null)
            {
                strSql1.Append("DateOther,");
                strSql2.Append("'" + model.DateOther + "',");
            }
            strSql1.Append("BranchID,");
            strSql2.Append("'" + model.BranchID + "',");
            if (model.IsDelete != null)
            {
                strSql1.Append("IsDelete,");
                strSql2.Append("'" + model.IsDelete + "',");
            }
            if (model.MemorialDay != null)
            {
                strSql1.Append("MemorialDay,");
                strSql2.Append("'" + model.MemorialDay + "',");
            }
            if (model.MemorialType != null)
            {
                strSql1.Append("MemorialType,");
                strSql2.Append("'" + model.MemorialType + "',");
            }
            if (model.HotelManage != null)
            {
                strSql1.Append("HotelManage,");
                strSql2.Append("'" + model.HotelManage + "',");
            }
            if (model.WeedPrice != null)
            {
                strSql1.Append("WeedPrice,");
                strSql2.Append("'" + model.WeedPrice + "',");
            }
            strSql.Append("insert into T_PUB_ORDER(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strArry.Add(strSql);
            #endregion

            #region 添加订单项目数据和日志信息
            string strOrder_projectSql;
            string strLogSql;
            foreach (KeyValuePair<string, string> item in ProjectID_No)
            {
                strOrder_projectSql = string.Format(" insert into T_PUB_Order_Project (OriderID,ProjectID,Number) VALUES('{0}','{1}','{2}')", model.ORDER_ID, item.Key, item.Value);
                strArry.Add(strOrder_projectSql);

                strLogSql = string.Format(@" insert into [WEDDING].[dbo].[T_PUB_ORDERLOG]  ([Order_ID] ,[Project_ID] ,[Date_Time] ,[User_ID],[EVENT])
                   VALUES('{0}','{1}',getdate(),'{2}','{3}') ", model.ORDER_ID, item.Key, model.Reception, "添加项目数量为：" + item.Value);
                strArry.Add(strLogSql);
            }
            #endregion

            #region  金额日志
            if (model.DingJin != null)
            {
                string strAmountLog = string.Format(@" INSERT INTO T_PUB_ORDER_AMOUNT_LOG ([Order_ID] ,[Money] ,[User_ID] ,[Date_Time] ,[Remark],[Envel])
                 VALUES ('{0}','{1}','{2}',getdate(),'第一次预交定金','定金')", model.ORDER_ID, model.DingJin, model.Reception);
                strArry.Add(strAmountLog);
            }
            DBHelper.ExecuteSqlTran(strArry);
            #endregion
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        /// <param name="coustomerModel"></param>
        /// <param name="strAddProject"></param>
        /// <param name="strDelProject"></param>
        /// <param name="strUserID"></param>
        /// <param name="dingjin"></param>
        public void EditOrder(Model.Order.T_PUB_ORDER model, Model.View.T_PUB_CUSTOMERINFO coustomerModel, IList<Model.Order.OrderProjectChange> changeModel, string strUserID, decimal dingjin)
        {
            ArrayList strArry = new ArrayList();

            #region 修改客户信息
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_CUSTOMERINFO set ");

            strSql.Append("GROOM_NAME='" + coustomerModel.GROOM_NAME + "',");

            strSql.Append("BRIDE_NAME='" + coustomerModel.BRIDE_NAME + "',");

            strSql.Append("GROOMPOST='" + coustomerModel.GROOMPOST + "',");

            strSql.Append("BRIDEPOST='" + coustomerModel.BRIDEPOST + "',");

            strSql.Append("GROOMPHONE='" + coustomerModel.GROOMPHONE + "',");

            strSql.Append("BRIDEPHONE='" + coustomerModel.BRIDEPHONE + "',");

            strSql.Append("GROOMQQ='" + coustomerModel.GROOMQQ + "',");


            strSql.Append("BRIDEQQ='" + coustomerModel.BRIDEQQ + "',");

            strSql.Append("GROOMAGE='" + coustomerModel.GROOMAGE + "',");

            strSql.Append("BRIDEAGE='" + coustomerModel.BRIDEAGE + "',");

            strSql.Append("HOUSEADDRESS='" + coustomerModel.HOUSEADDRESS + "',");

            strSql.Append("WEDDING='" + coustomerModel.WEDDING + "',");

            strSql.Append("BRIDEADDRESS='" + coustomerModel.BRIDEADDRESS + "',");

            strSql.Append("MARRIAGECAR='" + coustomerModel.MARRIAGECAR + "',");

            if (coustomerModel.TIME != null)
            {
                strSql.Append("TIME='" + coustomerModel.TIME + "',");
            }
            else
            {
                strSql.Append("TIME= null ,");
            }
            if (coustomerModel.HOTEL != null)
            {
                strSql.Append("HOTEL='" + coustomerModel.HOTEL + "',");
            }
            else
            {
                strSql.Append("HOTEL= null ,");
            }
            if (coustomerModel.TABLENUM != null)
            {
                strSql.Append("TABLENUM=" + coustomerModel.TABLENUM + ",");
            }
            else
            {
                strSql.Append("TABLENUM= null ,");
            }
            if (coustomerModel.RANK != null)
            {
                strSql.Append("RANK=" + coustomerModel.RANK + ",");
            }
            else
            {
                strSql.Append("RANK= null ,");
            }
            if (coustomerModel.ELDER != null)
            {
                strSql.Append("ELDER=" + coustomerModel.ELDER + ",");
            }
            else
            {
                strSql.Append("ELDER= null ,");
            }
            if (coustomerModel.YOUNGER != null)
            {
                strSql.Append("YOUNGER=" + coustomerModel.YOUNGER + ",");
            }
            else
            {
                strSql.Append("YOUNGER= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where CUSTOMER_ID='" + coustomerModel.CUSTOMER_ID + "'");
            strArry.Add(strSql);

            #endregion

            #region 修改订单
            StringBuilder strOrderSql = new StringBuilder();
            strOrderSql.Append("update T_PUB_ORDER set ");


            if (model.PROJECT_IDS != null)
            {
                strOrderSql.Append("PROJECT_IDS='" + model.PROJECT_IDS + "',");
            }
            else
            {
                strOrderSql.Append("PROJECT_IDS= null ,");
            }
            if (model.STYLE_IDS != null)
            {
                strOrderSql.Append("STYLE_IDS='" + model.STYLE_IDS + "',");
            }
            else
            {
                strOrderSql.Append("STYLE_IDS= null ,");
            }
            if (model.Reception != null)
            {
                strOrderSql.Append("Reception=" + model.Reception + ",");
            }
            else
            {
                strOrderSql.Append("Reception= null ,");
            }
            if (model.RealToal != null)
            {
                strOrderSql.Append("RealToal=" + model.RealToal + ",");
            }
            else
            {
                strOrderSql.Append("RealToal= null ,");
            }
            if (model.Total != null)
            {
                strOrderSql.Append("Total=" + model.Total + ",");
            }
            else
            {
                strOrderSql.Append("Total= null ,");
            }
            if (model.DingJin != null)
            {
                strOrderSql.Append("DingJin=" + model.DingJin + ",");
            }
            else
            {
                strOrderSql.Append("DingJin= null ,");
            }
            if (model.Type != null)
            {
                strOrderSql.Append("Type=" + model.Type + ",");
            }
            else
            {
                strOrderSql.Append("Type= null ,");
            }
            if (model.Other != null)
            {
                strOrderSql.Append("Other='" + model.Other + "',");
            }
            else
            {
                strOrderSql.Append("Other= null ,");
            }


            if (model.Source != null)
            {
                strOrderSql.Append("Source='" + model.Source + "',");
            }
            else
            {
                strOrderSql.Append("Source= null ,");
            }
            if (model.OrderDate != null)
            {
                strOrderSql.Append("OrderDate='" + model.OrderDate + "',");
            }
            else
            {
                strOrderSql.Append("OrderDate= null ,");
            }
            if (model.DateOther != null)
            {
                strOrderSql.Append("DateOther='" + model.DateOther + "',");
            }
            else
            {
                strOrderSql.Append("DateOther= null ,");
            }
            if (model.MemorialDay != null)
            {
                strOrderSql.Append("MemorialDay='" + model.MemorialDay + "',");
            }
            else
            {
                strOrderSql.Append("MemorialDay= null ,");
            }
            if (model.MemorialType != null)
            {
                strOrderSql.Append("MemorialType='" + model.MemorialType + "',");
            }
            else
            {
                strOrderSql.Append("MemorialType= null ,");
            }
            if (model.HotelManage != null)
            {
                strOrderSql.Append("HotelManage='" + model.HotelManage + "',");
            }
            else
            {
                strOrderSql.Append("HotelManage= null ,");
            }
            if (model.WeedPrice != null)
            {
                strOrderSql.Append("WeedPrice='" + model.WeedPrice + "',");
            }
            else
            {
                strOrderSql.Append("WeedPrice= null ,");
            }
            int index = strOrderSql.ToString().LastIndexOf(",");
            strOrderSql.Remove(index, 1);
            strOrderSql.Append(" where ORDER_ID='" + model.ORDER_ID + "' ");
            strArry.Add(strOrderSql);
            #endregion
            string strDelIDs = "";
            string strDelLogSql;
            string strOrder_projectAddSql;
            string strLogSql;
            string strOrder_projectUpdateSql;
            string strLogUpdateSql;
            foreach (Model.Order.OrderProjectChange item in changeModel)
            {
                if (item.strFlag == "0")  //删除
                {
                    strDelIDs = strDelIDs + "'" + item.ID + "',";
                    strDelLogSql = string.Format(@" insert into [WEDDING].[dbo].[T_PUB_ORDERLOG]  ([Order_ID] ,[Project_ID] ,[Date_Time] ,[User_ID],[EVENT])
                    VALUES('{0}','{1}',getdate(),'{2}','{3}') ", model.ORDER_ID, item.ProjectID, strUserID, "移除项目");
                    strArry.Add(strDelLogSql);
                }
                else if (item.strFlag == "1")   //添加
                {
                    strOrder_projectAddSql = string.Format(" insert into T_PUB_Order_Project (OriderID,ProjectID,Number) VALUES('{0}','{1}','{2}')", model.ORDER_ID, item.ProjectID, item.ProjectNum);
                    strArry.Add(strOrder_projectAddSql);

                    strLogSql = string.Format(@" insert into [WEDDING].[dbo].[T_PUB_ORDERLOG]  ([Order_ID] ,[Project_ID] ,[Date_Time] ,[User_ID],[EVENT])
                   VALUES('{0}','{1}',getdate(),'{2}','{3}') ", model.ORDER_ID, item.ProjectID, model.Reception, "添加项目数量为：" + item.ProjectNum);
                    strArry.Add(strLogSql);
                }
                else  //编辑
                {
                    strOrder_projectUpdateSql = string.Format(" update  T_PUB_Order_Project set Number={0} where id='{1}'", item.ProjectNum, item.ID);
                    strArry.Add(strOrder_projectUpdateSql);

                    strLogUpdateSql = string.Format(@" insert into [WEDDING].[dbo].[T_PUB_ORDERLOG]  ([Order_ID] ,[Project_ID] ,[Date_Time] ,[User_ID],[EVENT])
                   VALUES('{0}','{1}',getdate(),'{2}','{3}') ", model.ORDER_ID, item.ProjectID, model.Reception, "修改项目数量为：" + item.ProjectNum);
                    strArry.Add(strLogUpdateSql);
                }
            }
            if (strDelIDs!="")
            {
                strDelIDs = strDelIDs.Substring(0, strDelIDs.Length - 1);
                string strDelSql = string.Format(" delete T_PUB_Order_Project where id in ({0})", strDelIDs);
                strArry.Add(strDelSql);
            }
            if (dingjin != 0)
            {
                string strAmountLog = string.Format(@" INSERT INTO T_PUB_ORDER_AMOUNT_LOG ([Order_ID] ,[Money] ,[User_ID] ,[Date_Time] ,[Remark],[Envel])
                 VALUES ('{0}','{1}','{2}',getdate(),'修改订单定金','定金')", model.ORDER_ID, model.DingJin, strUserID);
                strArry.Add(strAmountLog);
            }
            DBHelper.ExecuteSqlTran(strArry);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetOrderList(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT o.ORDER_ID, \n");
            varname1.Append("       o.realtoal,o.realtoal-o.Total as Surplus,o.checkitem, \n");
            varname1.Append("       o.Total, \n");
            varname1.Append("       d.NAME, \n");
            varname1.Append("       o.OrderDate, \n");
            varname1.Append("       ( CASE o.DateOther \n");
            varname1.Append("           WHEN '1' THEN '上午' \n");
            varname1.Append("           WHEN '2' THEN '中午' \n");
            varname1.Append("           WHEN '3' THEN '下午' \n");
            varname1.Append("           WHEN '4' THEN '晚上' \n");
            varname1.Append("           ELSE '' \n");
            varname1.Append("         END ) AS dataother, \n");
            varname1.Append("       c.GROOM_NAME, \n");
            varname1.Append("       c.BRIDE_NAME \n");
            varname1.Append("FROM   dbo.T_PUB_ORDER o, \n");
            varname1.Append("       dbo.T_PUB_CUSTOMERINFO c, \n");
            varname1.Append("       dbo.T_PUB_DICTIONARY d \n");
            varname1.Append("WHERE  o.CUSTOMER_ID = c.CUSTOMER_ID \n");
            varname1.Append("       AND o.Type = d.ID \n");
            varname1.Append("       AND o.IsDelete != '1' ");
            if (strWhere != "")
            {
                varname1.Append(strWhere);
            }
            varname1.Append(" order by OrderDate desc");
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Order.T_PUB_ORDER GetModelByID(string ORDER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ORDER_ID,CUSTOMER_ID,Num,PROJECT_IDS,STYLE_IDS,Reception,RealToal,Total,DingJin,Type,Other,IsDelete,DeleteTime,DeleteUser,CheckItem,Source,OrderDate,DateOther,BranchID,MemorialDay,MemorialType,DeleteReason,HotelManage,WeedPrice from T_PUB_ORDER ");
            strSql.Append(" where ORDER_ID=@ORDER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.String, ORDER_ID);
            Model.Order.T_PUB_ORDER model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public bool Update(Model.Order.T_PUB_ORDER model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_ORDER set ");
            strSql.Append("CUSTOMER_ID=@CUSTOMER_ID,");
            strSql.Append("Num=@Num,");
            strSql.Append("PROJECT_IDS=@PROJECT_IDS,");
            strSql.Append("STYLE_IDS=@STYLE_IDS,");
            strSql.Append("Reception=@Reception,");
            strSql.Append("RealToal=@RealToal,");
            strSql.Append("Total=@Total,");
            strSql.Append("DingJin=@DingJin,");
            strSql.Append("Type=@Type,");
            strSql.Append("Other=@Other,");
            strSql.Append("IsDelete=@IsDelete,");
            strSql.Append("DeleteTime=@DeleteTime,");
            strSql.Append("DeleteReason=@DeleteReason,");
            strSql.Append("DeleteUser=@DeleteUser,");
            strSql.Append("CheckItem=@CheckItem,");
            strSql.Append("Source=@Source,");
            strSql.Append("OrderDate=@OrderDate,");
            strSql.Append("DateOther=@DateOther,");
            strSql.Append("BranchID=@BranchID,");
            strSql.Append("MemorialDay=@MemorialDay,");
            strSql.Append("MemorialType=@MemorialType");
            strSql.Append(" where ORDER_ID=@ORDER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.String, model.ORDER_ID);
            db.AddInParameter(dbCommand, "CUSTOMER_ID", DbType.String, model.CUSTOMER_ID);
            db.AddInParameter(dbCommand, "Num", DbType.String, model.Num);
            db.AddInParameter(dbCommand, "PROJECT_IDS", DbType.String, model.PROJECT_IDS);
            db.AddInParameter(dbCommand, "STYLE_IDS", DbType.String, model.STYLE_IDS);
            db.AddInParameter(dbCommand, "Reception", DbType.Int32, model.Reception);
            db.AddInParameter(dbCommand, "RealToal", DbType.Double, model.RealToal);
            db.AddInParameter(dbCommand, "Total", DbType.Double, model.Total);
            db.AddInParameter(dbCommand, "DingJin", DbType.Double, model.DingJin);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, model.Type);
            db.AddInParameter(dbCommand, "Other", DbType.String, model.Other);
            db.AddInParameter(dbCommand, "IsDelete", DbType.String, model.IsDelete);
            db.AddInParameter(dbCommand, "DeleteTime", DbType.String, model.DeleteTime);
            db.AddInParameter(dbCommand, "DeleteReason", DbType.String, model.DeleteReason);
            db.AddInParameter(dbCommand, "DeleteUser", DbType.Int32, model.DeleteUser);
            db.AddInParameter(dbCommand, "CheckItem", DbType.String, model.CheckItem);
            db.AddInParameter(dbCommand, "Source", DbType.String, model.Source);
            db.AddInParameter(dbCommand, "OrderDate", DbType.String, model.OrderDate);
            db.AddInParameter(dbCommand, "DateOther", DbType.String, model.DateOther);
            db.AddInParameter(dbCommand, "BranchID", DbType.Int32, model.BranchID);
            db.AddInParameter(dbCommand, "MemorialDay", DbType.String, model.MemorialDay);
            db.AddInParameter(dbCommand, "MemorialType", DbType.Int32, model.MemorialType);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Order.T_PUB_ORDER ReaderBind(IDataReader dataReader)
        {
            Model.Order.T_PUB_ORDER model = new Model.Order.T_PUB_ORDER();
            object ojb;
            model.ORDER_ID = dataReader["ORDER_ID"].ToString();
            model.CUSTOMER_ID = dataReader["CUSTOMER_ID"].ToString();
            model.Num = dataReader["Num"].ToString();
            model.PROJECT_IDS = dataReader["PROJECT_IDS"].ToString();
            model.STYLE_IDS = dataReader["STYLE_IDS"].ToString();
            ojb = dataReader["Reception"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Reception = (int)ojb;
            }
            ojb = dataReader["RealToal"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RealToal = Convert.ToDecimal(ojb);
            }
            ojb = dataReader["Total"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Total = Convert.ToDecimal(ojb);
            }
            ojb = dataReader["DingJin"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DingJin = Convert.ToDecimal(ojb);
            }
            ojb = dataReader["Type"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Type = (int)ojb;
            }
            model.Other = dataReader["Other"].ToString();
            model.IsDelete = dataReader["IsDelete"].ToString();
            ojb = dataReader["DeleteTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeleteTime = (DateTime)ojb;
            }
            ojb = dataReader["DeleteUser"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeleteUser = (int)ojb;
            }
            model.CheckItem = dataReader["CheckItem"].ToString();
            model.Source = dataReader["Source"].ToString();
            ojb = dataReader["OrderDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OrderDate = (DateTime)ojb;
            }
            model.DateOther = dataReader["DateOther"].ToString();
            ojb = dataReader["BranchID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BranchID = (int)ojb;
            }
            ojb = dataReader["MemorialDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MemorialDay = (DateTime)ojb;
            }
            ojb = dataReader["MemorialType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MemorialType = (int)ojb;
            }
            model.DeleteReason = dataReader["DeleteReason"].ToString();
            model.HotelManage = dataReader["HotelManage"].ToString();
            model.WeedPrice = dataReader["WeedPrice"].ToString();
            return model;
        }

        public bool UpataeCount(string strID)
        {
            string strSql = string.Format(@"update  dbo.T_PUB_PROJECT set count=count+1
where PROJECT_ID in(
select ProjectID from dbo.T_PUB_Order_Project where OriderID='{0}')", strID);
             Database db = DatabaseFactory.CreateDatabase();
             DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #region 日志操作


        /// <summary>
        /// 交款添加记录
        /// </summary>
        /// <param name="money"></param>
        /// <param name="strOrderID"></param>
        /// <param name="strUserID"></param>
        /// <param name="strOther"></param>
        public void UpdateMoney(decimal money, string strOrderID, string strUserID, string strOther)
        {
            ArrayList strArry = new ArrayList();
            string strUpateOrder = string.Format(" update T_PUB_ORDER set total=total+{0} where order_id='{1}'", money, strOrderID);
            strArry.Add(strUpateOrder);
            string strAmountLog = string.Format(@" INSERT INTO T_PUB_ORDER_AMOUNT_LOG ([Order_ID] ,[Money] ,[User_ID] ,[Date_Time] ,[Remark],[Envel])
                 VALUES ('{0}','{1}','{2}',getdate(),'{3}','交款')", strOrderID, money, strUserID, strOther);
            strArry.Add(strAmountLog);
            DBHelper.ExecuteSqlTran(strArry);
        }

        //获得交款日志
        public DataTable GetAmountLog(string strOrderID)
        {
            string strSql = string.Format(" select T_PUB_ORDER_AMOUNT_LOG.*,T_PUB_USER.NAME from dbo.T_PUB_ORDER_AMOUNT_LOG,dbo.T_PUB_USER where T_PUB_ORDER_AMOUNT_LOG.User_ID=T_PUB_USER.USER_ID and order_id='{0}'  order by Date_Time", strOrderID);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        //项目日志

        public DataTable GetProjectLog(string strOrderID)
        {
            string strSql = string.Format(@"select T_PUB_ORDERLOG.*,T_PUB_PROJECT.PROJECT_NAME,T_PUB_USER.NAME 
                                             from dbo.T_PUB_ORDERLOG,dbo.T_PUB_PROJECT,dbo.T_PUB_USER
                                             where T_PUB_ORDERLOG.Project_ID=T_PUB_PROJECT.PROJECT_ID
                                             and T_PUB_ORDERLOG.User_ID=T_PUB_USER.USER_ID and T_PUB_ORDERLOG.order_id='{0}'", strOrderID);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        #endregion

       
    }
}
